<?php


namespace App\Http\Controllers\Admin;


use App\Http\Requests\Admin\Auth\ChangePasswordRequest;
use Illuminate\Http\Request;

class AuthController extends BaseController
{
    /**
     * 登录
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function login(Request $request)
    {
        $credentials = $request->only('username', 'password');
        if (!$this->auth()->attempt(
            [
                'username' => $credentials['username'],
                'password' => $credentials['password'],
            ]
        )) {
            return $this->error('用户名或密码错误');
        }
        $user = $this->auth()->user();
        if (!$user->status) {
            return $this->error('用户已被禁用');
        }
        return $this->success([], '登录成功');
    }

    /**
     * 获取个人信息
     * @return \Illuminate\Http\JsonResponse
     */
    public function info()
    {
        $user = $this->auth()->user();
        $menus = config('admin.nav');

        return $this->success(compact('user','menus'));
    }
    public function changePassword(ChangePasswordRequest $request)
    {
        $user = $this->auth()->user();
        $user->password = $request->get('password');
        return $user->save()?$this->success([],'修改成功'):$this->error('修改失败');
    }

    /**
     * 注销
     * @return \Illuminate\Http\JsonResponse
     */
    public function logout()
    {
        $this->auth()->logout();
        return $this->success([],'退出成功');
    }
}
